package b4.view.component.town
{
	import as3isolib.display.IsoSprite;
	import as3isolib.geom.Pt;
	
	import b4.config.AppearanceConfig;
	import b4.config.Config;
	import b4.config.LayerConfig;
	import b4.config.LayerItemConfig;
	import b4.util.Resource;
	import b4.view.component.events.RoleEvent;
	
	import flash.events.Event;

	public class RoleSprite extends IsoSprite
	{
		private static const gridWidth:int = 100;
		private static const speed:Number = 10.5;
		private var appearanceSprite:IsoSprite;
		private var path:Array;
		private var layer:LayerConfig;
		private var suspendWalkFlag:Boolean;
		private var lastLayerItem:LayerItemConfig;
		
		public function RoleSprite()
		{
			super();
			appearanceSprite = new IsoSprite();
			appearanceSprite.setSize(gridWidth, gridWidth, gridWidth);
			addChild(appearanceSprite);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		public function stopWalk():void
		{
			this.path = null;
		}
		
		public function suspendWalk():void
		{
			this.suspendWalkFlag = true;
		}
		
		public function resumeWalk():void
		{
			this.suspendWalkFlag = false;
		}
		
		public function getCurrectLayerItem():LayerItemConfig
		{
			var posSTD:Pt = PathFinding.posToSTD(new Pt(this.x, this.y));
			return layer != null ? layer.map[posSTD.x + " " + posSTD.y] : null;
		}
		
		private function onAddedToStage(event:Event):void
		{
			this.addEventListener(Event.ENTER_FRAME, onTick);
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			this.removeEventListener(Event.ENTER_FRAME, onTick);
		}
		
		private function onTick(event:Event):void
		{
			if (!suspendWalkFlag && path != null && path.length > 0)
			{
				var dest:Pt = path[path.length - 1];
				var dist:Number = Math.sqrt((dest.x - this.x) * (dest.x - this.x) + (dest.y - this.y) * (dest.y - this.y) 
					- 2 * (dest.x - this.x) * (dest.y - this.y) * Math.cos(Math.atan(0.5) * 2));
				if (dist > speed * 1.2)
				{
					var directX:Number = (dest.x - this.x) / dist;
					var directY:Number = (dest.y - this.y) / dist;
					this.x += speed * directX;
					this.y += speed * directY;
					var currentLayerItem:LayerItemConfig = getCurrectLayerItem();
					if (lastLayerItem != currentLayerItem)
					{
						var roleEvent:RoleEvent = new RoleEvent(RoleEvent.ON_REACH_LAYER_ITEM);
						roleEvent.data = currentLayerItem;
						dispatchEvent(roleEvent);
						lastLayerItem = currentLayerItem;
					}
					roleEvent = new RoleEvent(RoleEvent.ON_POSITION_CHANGE);
					roleEvent.data = new Pt(this.x, this.y);
					dispatchEvent(roleEvent);
					lastLayerItem = currentLayerItem;
				}
				else
				{
					path.pop();
				}			
			}
		}
		
		public function setAppearance(appearanceIndex:int):void
		{
			var appearanceConfig:AppearanceConfig = Config.appearanceConfigs[appearanceIndex];
			var AppearanceIcon:Class = Resource.getClassByName("HumanBeingIcon");//appearanceConfig.iconImg);
			appearanceSprite.sprites = [AppearanceIcon];
		}
		
		public function setDestination(layer:LayerConfig, destX:Number, destY:Number):void
		{
			if (layer != null)
			{
				this.layer = layer;
				path = PathFinding.findPathByAstar(layer, new Pt(this.x, this.y), new Pt(destX, destY));
			}
		}
		
	}
}